package com.zys.el.user.util.time;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 种群个体
 */
@Data
@AllArgsConstructor
public class Schedule {

    private String taskId;

    /**
     * 基因序列
     */
    private List<Integer> slot; // 索引：0~47, 48~106, 107~130

    // 影响因子
    private int time;
    private int lenSlot;

    public Schedule(String taskId, int time) {
        this.taskId = taskId;
        this.time = time;
    }

    public Schedule() {
    }

    public Schedule(String teachTaskId) {
        this.taskId = teachTaskId;
    }

    void random_init(Integer scheduleNum) {
        //这里随机初始值的时候 还是使用一周五天 一天4节课的正常 课程安排条件
        Random random = new Random();
//        this.classroomId = roomRange.get(random.nextInt(roomRange.size()));
//        this.weekday = 1 + random.nextInt(7);
        int idx = random.nextInt(scheduleNum - 1 + 1);
//        int timeLen = 1 + random.nextInt(taskNum - idx + 1);
        lenSlot = time / MyGa.timeInterval;

        ArrayList<Integer> integers = new ArrayList<>();
        if (lenSlot + idx > scheduleNum)
            lenSlot -= lenSlot + idx - scheduleNum;
        for (int i = 0; i < lenSlot; i++) {
            integers.add(idx++);
        }
        this.slot = integers;

    }

    // 是否冲突
    public static boolean hasConflict(List<Schedule> schedules) {
        for (int i = 0; i < schedules.size(); i++) {
            Schedule schedule1 = schedules.get(i);
            List<Integer> s1 = schedule1.getSlot();
            int size1 = s1.size();

            for (int j = i + 1; j < schedules.size(); j++) {
                Schedule schedule2 = schedules.get(j);
                List<Integer> s2 = schedule2.getSlot();

                int size2 = s2.size();
                if ((s1.get(0) > s2.get(0) && s1.get(0) < s2.get(size2 - 1))
                        || (s1.get(size1 - 1) > s2.get(0) && s1.get(size1 - 1) < s2.get(size2 - 1))
                        || (s1.get(0) < s2.get(0) && s1.get(size1 - 1) > s2.get(size2 - 1))
                        || (s1.get(0) > s2.get(0) && s1.get(size1 - 1) < s2.get(size2 - 1))

                ) {

                    return true;
                }
            }
        }
        return false;
    }

    // 计算适应度
    public void caleRate() {

    }


}